package lxh.palallaxwelcomeview.parallax;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import lxh.palallaxwelcomeview.R;

/**
 * Created by liuxh on 2017/8/13.
 */

public class ParallaxViewPager extends ViewPager {
    private List<ParallaxFragment> mFragmentList;
    public ParallaxViewPager(Context context) {
        this(context,null);
    }

    public ParallaxViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mFragmentList=new ArrayList<>();
    }

    public void setLayout(FragmentManager fm, int[] layoutId) {
        //初始化fragment
        for (int i = 0; i < layoutId.length; i++) {
            ParallaxFragment fragment=new ParallaxFragment();
            Bundle bundle=new Bundle();
            bundle.putInt(ParallaxFragment.LAYOUT_ID,layoutId[i]);
            fragment.setArguments(bundle);
            mFragmentList.add(fragment);
        }
        //setAdapter
        setAdapter(new ParallaxAdapter(fm));
        //设置监听
        addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // 获取左out 右 in
                ParallaxFragment outFragment = mFragmentList.get(position);
                List<View> parallaxViews = outFragment.getParallaxViews();
                for (View parallaxView : parallaxViews) {
                    ParallaxTag tag = (ParallaxTag) parallaxView.getTag(R.id.parallax_tag);

                    parallaxView.setTranslationX((-positionOffsetPixels)*tag.translationXOut);
                    parallaxView.setTranslationY((-positionOffsetPixels)*tag.translationYOut);
                }

                try {
                    ParallaxFragment inFragment = mFragmentList.get(position+1);
                    parallaxViews = inFragment.getParallaxViews();
                    for (View parallaxView : parallaxViews) {
                        ParallaxTag tag = (ParallaxTag) parallaxView.getTag(R.id.parallax_tag);
                        parallaxView.setTranslationX((getMeasuredWidth()-positionOffsetPixels)*tag.translationXIn);
                        parallaxView.setTranslationY((getMeasuredWidth()-positionOffsetPixels)*tag.translationYIn);
                    }
                }catch (Exception e){}
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }




    class ParallaxAdapter extends FragmentPagerAdapter{

        public ParallaxAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList!=null?mFragmentList.size():0;
        }
    }
}
